package com.dtsz.m2.ui.activity;


import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.android.volley.Request;
import com.dtsz.m2.R;
import com.dtsz.m2.config.UrlConfig;
import com.dtsz.m2.refresh.LoadingFooter;
import com.dtsz.m2.ui.adapter.InvitationNumberAdatper;
import com.dtsz.m2.ui.base.BaseActivity;
import com.dtsz.m2.ui.model.Users;
import com.dtsz.m2.volley.state.HUCState;

import java.util.ArrayList;
import java.util.List;

/**
 * 邀约人数
 */
public class InvitationNumberActivity extends BaseActivity implements com.dtsz.m2.refresh.xRecyclerView.OnRefreshListener {
    private com.dtsz.m2.refresh.xRecyclerView xRecyclerView;
    private InvitationNumberAdatper noticeAdatper;
    private List<Users> usersList = new ArrayList<>();
    private boolean refreshFlag = true;

    private TextView tvPhone, tvCode, tvCertification, tvCount, tvOpen, tvOpenEnd;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        setContentView(R.layout.activity_invitation_number);
        super.onCreate(savedInstanceState);
        tvActionbarTitle.setText("邀请人数");
        noticeAdatper = new InvitationNumberAdatper(this, usersList, R.layout.item_invitation_number);
        xRecyclerView.setAdapter(this, noticeAdatper, new LinearLayoutManager(this));
        xRecyclerView.setOnRefreshListener(this);
    }

    @Override
    public void initClick() {
        super.initClick();
        findViewById(R.id.tv_yq).setOnClickListener(this);
    }

    @Override
    public void initView() {
        super.initView();
        xRecyclerView = findViewById(R.id.xrecyclerView);
        tvPhone = findViewById(R.id.tv_phone);
        tvCode = findViewById(R.id.tv_code);
        tvCount = findViewById(R.id.tv_count);
        tvOpen = findViewById(R.id.tv_open);
        tvOpenEnd = findViewById(R.id.tv_open_end);
        tvCertification = findViewById(R.id.tv_certification);
        tvOpenEnd.setOnClickListener(this);
        tvOpen.setOnClickListener(this);
    }

    /**
     * 下拉刷新
     *
     * @param refreshLayout
     */
    @Override
    public void refresh(SwipeRefreshLayout refreshLayout) {
        refreshFlag = false;
        if (usersList != null) {
            usersList.clear();
        }
        pageVo.setCurrent(1 + "");
        pageVo.setSize(10 + "");
        getInitDatas();
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.tv_yq:
                initData();
                break;
            case R.id.tv_open:
                tvOpen.setTextColor(getResources().getColor(R.color.colorFont11));
                tvOpenEnd.setTextColor(getResources().getColor(R.color.colorFont2));
                if (usersList != null) {
                    usersList.clear();
                }
                pageVo.setCurrent(1 + "");
                pageVo.setSize(10 + "");
                pageVo.setIndex("1");
                getInitDatas();
                break;
            case R.id.tv_open_end:
                tvOpenEnd.setTextColor(getResources().getColor(R.color.colorFont11));
                tvOpen.setTextColor(getResources().getColor(R.color.colorFont2));
                if (usersList != null) {
                    usersList.clear();
                }
                pageVo.setCurrent(1 + "");
                pageVo.setSize(10 + "");
                pageVo.setIndex("0");
                getInitDatas();
                break;
        }
    }

    /**
     * 上拉加载
     *
     * @param recyclerView
     */
    @Override
    public void loadMore(RecyclerView recyclerView) {
        refreshFlag = true;
        int CurrentCount = Integer.parseInt(pageVo.getCurrent()) + 1;
        pageVo.setCurrent(CurrentCount + "");
        pageVo.setSize(10 + "");
        getInitDatas();
    }

    @Override
    public void onSuccessfulStrType(int inputType, Object obj) {
        switch (inputType) {
            case HUCState.HUC_REQEUST_CODE_1:
                if (obj instanceof List) {
                    List<Users> users = (List<Users>) obj;
                    if (refreshFlag) {
                        if (users == null || users.size() == 0) {
                            if (usersList.size() == 0) {
                                xRecyclerView.refreshComplete(LoadingFooter.State.NoData, "");
                            } else {
                                xRecyclerView.refreshComplete(LoadingFooter.State.TheEnd, "");
                            }

                        } else {
                            for (Users model : users) {
                                usersList.add(model);
                            }
                            xRecyclerView.refreshComplete(LoadingFooter.State.Normal, "");
                        }
                    } else {
                        usersList.clear();
                        for (Users model : users) {
                            usersList.add(model);
                        }
                        xRecyclerView.refreshComplete(LoadingFooter.State.Normal, "");
                    }
                    noticeAdatper.notifyDataSetChanged();
                }
                break;
            case HUCState.HUC_REQEUST_CODE_2:
                if (obj instanceof Users) {
                    Users users = (Users) obj;
                    tvPhone.setText(users.getBuPhone());
                    tvCertification.setText("1".equals(users.getBuCertification()) ? "已实名" : "未实名");
                    tvCount.setText(users.getCount());
                }
                break;
            case HUCState.HUC_REQEUST_CODE_3:
                if (obj instanceof Users) {
                    Users user = (Users) obj;
                    Intent intentUrl = new Intent();
                    intentUrl.setClass(this, QrcodeActivity.class);
                    intentUrl.putExtra("URL", user.getBuQrcode());
                    intentUrl.putExtra("URLCode", user.getBuCode());
                    startActivity(intentUrl);
                } else {
                    Toast.makeText(this, obj.toString(), Toast.LENGTH_SHORT).show();
                }
                break;

        }
        loadDismiss();
    }

    @Override
    protected void onStart() {
        super.onStart();
        refreshFlag =false;
        pageVo.setCurrent(1 + "");
        pageVo.setSize(10 + "");
        pageVo.setIndex("1");
        getInitDatas();
        getInitData();
    }

    private void getInitDatas() {
        loading();
        hucRequestInterfase.executeStr(this, Users.class,
                HUCState.HUC_REQEUST_CODE_1, Request.Method.POST, UrlConfig.BIZ_INVITATION_USERS_URL,
                hucParameInterfase.getRequestParameter(pageVo), false);
    }

    private void getInitData() {
        hucRequestInterfase.executeStr(this, Users.class,
                HUCState.HUC_REQEUST_CODE_2, Request.Method.POST, UrlConfig.BIZ_INVITATION_USER_URL,
                hucParameInterfase.getRequestParameter(pageVo), true);
    }

    private void initData() {
        hucRequestInterfase.executeStr(this, Users.class,
                HUCState.HUC_REQEUST_CODE_3, Request.Method.POST, UrlConfig.USERS_URL,
                hucParameInterfase.getRequestParameter(pageVo), true);
    }
}
